﻿Public Partial Class _Default3
    Inherits System.Web.UI.Page

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If (Page.Request.ServerVariables("http_user_agent").ToLower().Contains("chrome")) _
        Or (Page.Request.ServerVariables("http_user_agent").ToLower().Contains("safari")) Then
            Page.ClientTarget = "uplevel"
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            carregaInformacoes()
        End If
    End Sub

    Public Sub carregaInformacoes()
        encheNoticiaEmDestaque()
        encheBannerGrande()
        encheBannerMedio00()
        encheBannerMedio01()
        encheAlbums()
        encheHistorico()
        encheFotosTunelTempo()
    End Sub
    Protected Sub encheNoticiaEmDestaque()
        Dim c As New SiteDLL.Noticia
        Try
            Dim dt As New DataTable
            dt = c.executarQuerySQL("SELECT TOP 1 * FROM Noticias where Imagem <> '' ORDER BY IDNoticia DESC;")

            Me.hplNoticiaTitulo.Text = dt.Rows(dt.Rows.Count - 1).Item(1).ToString
            Me.hplNoticiaManchete.Text = "- " & dt.Rows(dt.Rows.Count - 1).Item(2).ToString
            Me.imgNoticiaDestaque.ImageUrl = dt.Rows(dt.Rows.Count - 1).Item(5).ToString

            Me.hplNoticiaTitulo.NavigateUrl = "VisualizaNoticia.aspx?IDnoticia=" & dt.Rows(dt.Rows.Count - 1).Item(0).ToString
            Me.hplNoticiaManchete.NavigateUrl = "VisualizaNoticia.aspx?IDnoticia=" & dt.Rows(dt.Rows.Count - 1).Item(0).ToString
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Protected Sub encheBannerGrande()
        Dim c As New SiteDLL.Banner
        Try
            c.addWhere("Fixo", ClasseBase.Classes.PersistClass.TipoFiltro.Igual, "Sim")
            c.addWhere(ClasseBase.Classes.PersistClass.OperadorLogico.AND, "Tamanho", ClasseBase.Classes.PersistClass.TipoFiltro.Igual, "G")
            dtlBannerGrande.DataSource = c.getTodosRegistros
            dtlBannerGrande.DataBind()
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Protected Sub encheBannerMedio00()
        Dim c As New SiteDLL.Banner
        Try
            dtlBannerMedio00.DataSource = c.executarQuerySQL(" SELECT TOP 2 * FROM Banner WHERE Banner.Tamanho = 'M' ")
            dtlBannerMedio00.DataBind()
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Protected Sub encheBannerMedio01()
        Dim c As New SiteDLL.Banner
        Try
            dtlBannerMedio01.DataSource = c.executarQuerySQL(" SELECT " & _
                                                             "   Banner.Img, " & _
                                                             "   Banner.Url, " & _
                                                             "   Banner.ToolTip " & _
                                                             " FROM Banner " & _
                                                             " WHERE IDBanner NOT IN (Select TOP 2 Banner.IDBanner FROM Banner WHERE Banner.Tamanho = 'M')" & _
                                                             " AND Banner.Tamanho = 'M'")
            dtlBannerMedio01.DataBind()
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Private Sub encheAlbums()
        Dim c As New SiteDLL.Album
        Try
            dtListAlbuns.DataSource = c.executarQuerySQL(" SELECT TOP 6 " & _
                                                         "   Album.IDAlbum, " & _
                                                         "   (Select TOP 1 Fotos.Link FROM Fotos WHERE Fotos.IDAlbum = Album.IDAlbum) as Link, " & _
                                                         " Album.Nome " & _
                                                         " FROM Album ORDER BY Album.IDAlbum Desc ")
            dtListAlbuns.DataBind()
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Private Sub encheHistorico()
        Dim c As New SiteDLL.Noticia
        Try
            dtListHistorico.DataSource = c.executarQuerySQL(" SELECT TOP 6   " & _
                                                            "   IDNoticia, " & _
                                                            "   Titulo" & _
                                                            " FROM Noticias " & _
                                                            " WHERE HistoriaICLI=true")
            dtListHistorico.DataBind()
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub

    Private Sub encheFotosTunelTempo()
        Dim c As New SiteDLL.Fotos
        Try
            'Método Temporario
            'dtListTunelTempo.DataSource = c.executarQuerySQL(" SELECT TOP 6 " & _
            '                                                 "   IDFoto, " & _
            '                                                 "   IDAlbum, " & _
            '                                                 "   Descricao, " & _
            '                                                 "   Link " & _
            '                                                 " FROM Fotos " & _
            '                                                 " WHERE IDAlbum=34")
            'Método correto
            dtListTunelTempo.DataSource = c.executarQuerySQL(" SELECT TOP 6 " & _
                                                             "   Album.IDAlbum, " & _
                                                             "   Album.Nome, " & _
                                                             "   (Select TOP 1 Fotos.Link FROM Fotos WHERE Fotos.IDAlbum = Album.IDAlbum) as Link, " & _
                                                             " Album.Nome " & _
                                                             " FROM Album " & _
                                                             " WHERE HistoriaICLI=true" & _
                                                             " ORDER BY IDAlbum DESC ")

            dtListTunelTempo.DataBind()
        Catch ex As Exception
            Throw ex
        Finally
            c.Dispose()
            c = Nothing
        End Try
    End Sub
End Class